package lfr.ustc.ftp;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.internet.MimeUtility;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.Toast;

public class MailManger extends Activity {

	private String[] sendTo = { "522590434@qq.com" };
	private String account = "****@126.com"; //use you accout and pwd
	private String pwd = "****";
	private String host = "smtp.126.com"; // ���Ͷ˷��������û�����Ĳ�ͬ����������ı�
	private String port = "25"; // ���Ͷ˿ڣ�����û�����Ĳ�ͬ����������ı�

	private WifiManager wifiMgr = null;

	protected static final int GUI_SEND_NOTIFIER = 0x108;
	int progress_count = 0;

	EditText txt_title = null;
	EditText edt_content = null;
	TableLayout tl = null;

	private void initialize() {
		txt_title = (EditText) this.findViewById(R.id.edt_title);
		edt_content = (EditText) this.findViewById(R.id.edt_content);
		wifiMgr =

		(WifiManager) getSystemService(Context.WIFI_SERVICE);
		int wifiState =

		wifiMgr.getWifiState();
		if (wifiState ==

		WifiManager.WIFI_STATE_ENABLED) {
			new Thread(new Runnable() // ���߳�4���ƽ��
					{
						public void run() {
							SendMail send = SendMail.getInsatance();
							send.setProg(account, pwd, host, port);

							send.connect();
						}
					}).start();
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.writeemail);

		initialize();
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent();
			intent.setClass(this, StartServer.class);
			startActivity(intent);
			this.finish();
		} else {
			return super.onKeyDown(keyCode, event);
		}
		return false;
	}
	private void finishOfMain(){
		this.finish();
	}
	/**
	 * 验证邮箱格式是否合法
	 * 
	 * @param email
	 */
	public static boolean isValidate(String email) {
		boolean isRightEmail = true;
		final String emailFormat ="^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		final Pattern pattern = Pattern.compile(emailFormat);
		final Matcher mat = pattern.matcher(email);
		if (!mat.find()) {
			isRightEmail = false;
		}
		return isRightEmail;
	}
	public void onClickButton(View v) {

		switch (v.getId()) {
		case R.id.btn_send: {
			String title = txt_title.getText().toString().trim();
			String content = edt_content.getText().toString().trim();
			if(title.equals("")||title==null){
				Toast.makeText(this, "请填写你的邮箱，以便我们回复您", 3000).show();
				return;
			}else if(!isValidate(title)){
				Toast.makeText(this, "邮箱地址不合法，请重新填写", 3000).show();
				return;
				
			}
			if(content.equals("")||content==null){
				Toast.makeText(this, "请填写您要发送的内容", 3000).show();
				return;
			}
			wifiMgr =

			(WifiManager) getSystemService(Context.WIFI_SERVICE);
			int wifiState =

			wifiMgr.getWifiState();
			if (wifiState ==

			WifiManager.WIFI_STATE_ENABLED) {

				
				SendMail.getInsatance().setMessage(sendTo, title, content,
						"fruong");
				SendMail.getInsatance().start(true);

				final ProgressDialog pd = new ProgressDialog(this);
				pd.setMax(100);
				pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// ���ˮƽ��ʾ
				pd.show();
				new Thread(new Runnable() // ���߳�4���ƽ��
						{
							public void run() {
								try {
									while (progress_count <= 100) {
										pd.setProgress(progress_count++);
										Thread.sleep(30);
									}
									Message msg = new Message();
									msg.what = MailManger.GUI_SEND_NOTIFIER;
									MailManger.this.mHandler.sendMessage(msg);
									pd.cancel();
								} catch (InterruptedException e) {
									pd.cancel();
								}
							}
						}).start();
			} else {
				AlertDialog alert = null;
				Builder builder = new Builder(MailManger.this);
				builder.setMessage("没有开启无线网络，不能发送");
				builder.setPositiveButton("确定", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent();
						intent.setClass(MailManger.this, StartServer.class);
						startActivity(intent);
						MailManger.this.finish();
						dialog.cancel();
					}
				});
				alert = builder.create();
				alert.show();
			}

			break;
		}

		case R.id.btn_return: {
			Intent intent_login = new Intent();
			intent_login.setClass(MailManger.this, StartServer.class);
			startActivity(intent_login);
			MailManger.this.finish();
			break;
		}
		case R.id.btn_reset2: {
			edt_content.setText("");
			break;
		}

		}

	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MailManger.GUI_SEND_NOTIFIER:
				AlertDialog alert = null;
				Builder builder = new Builder(MailManger.this);
				builder.setMessage("发送完毕");
				builder.setPositiveButton("确定", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent();
						intent.setClass(MailManger.this, StartServer.class);
						startActivity(intent);
						MailManger.this.finish();
						dialog.cancel();
					}
				});
				alert = builder.create();
				alert.show();
				break;
			}
			super.handleMessage(msg);
		}
	};
}
